package com.hzx.demo.activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hzx.demo.R;
import com.hzx.demo.util.Entry;
import com.hzx.demo.util.HZXLog;

/**
 * Created by ldkxingzhe on 2016/5/13.
 */
@Entry(
        desc = "1.练习使用Dialog和PopupWindow\n2.打印Log, 观察dialog弹出阶段的activity声明周期",
        createTime = "2016-05-13 23:59"
)
public class DialogAndPopupWindowActivity extends AppCompatActivity{
    @SuppressWarnings("unused")
    private static final String TAG = "DialogAndPopupWindowActivity";

    private Button mAlertDialogButton;
    private Button mPopWindowButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog_activity);
        mAlertDialogButton = (Button) findViewById(R.id.btn_alert_dialog);
        mPopWindowButton = (Button) findViewById(R.id.btn_pop_window);
        mAlertDialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });
        mPopWindowButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWindow();
            }
        });
        HZXLog.v(TAG, "onCreate");
    }

    private void showPopWindow() {
        HZXLog.v(TAG, "in function showPopWindow");
        PopupWindow popupWindow = new PopupWindow(this);
        TextView textView = new TextView(this);
        textView.setText("1. 观察对应的生命周期,\n2. 与Dialog是否有不同\n3.上网搜索两者");
        textView.setBackgroundColor(Color.GRAY);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(textView);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(mPopWindowButton);
    }

    @Override
    protected void onResume() {
        super.onResume();
        HZXLog.v(TAG, "onResume");
    }

    private void showAlertDialog() {
        HZXLog.v(TAG, "in function showAlertDialog");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("A Test For AlertDialog");
        builder.setMessage("测试AlertDialog, 主要是观察Activity的生命周期. \n" +
                "onPause --> onResume?\n" +
                "是否调用了onStop 和 onCreate? 为什么?");
        builder.setPositiveButton("知道了", null);
        builder.show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        HZXLog.v(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        HZXLog.v(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        HZXLog.v(TAG, "onDestroy");
        super.onDestroy();
    }
}
